home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / TravelingSalesman / Source_code / PlotController.m < prev    next >
Encoding:
Text File  |  1991-09-03  |  3.0 KB  |  116 lines

  1.  
  2. /* 
  3.  * PlotController.m -- Implementation file for the PlotController class 
  4.  *
  5.  * You may freely copy, distribute, and reuse the code in this example.
  6.  * NeXT disclaims any warranty of any kind, expressed or implied, as to its
  7.  * fitness for any particular use.
  8.  *
  9.  */
  10.  
  11. #import "PlotController.h"
  12. #import "PlotView.h"
  13. #import <appkit/Application.h>
  14. #import <appkit/Listener.h>
  15. #import <appkit/Pasteboard.h>
  16. #import <appkit/Window.h>
  17. #import <appkit/ScrollView.h>
  18. #import <appkit/Text.h>
  19. #import <stdlib.h> 
  20. #import <string.h> 
  21. #include <mach.h>
  22.  
  23. @implementation PlotController
  24.  
  25. - appDidInit:sender 
  26. /*
  27.  * Responds to a message that's sent just before the application
  28.  * gets the first event.  PlotController initializes its inputText
  29.  * instance variable.  It makes itself the inputText's delegate and
  30.  * the services delegate for the application.  It also ensures that 
  31.  * the application's single window becomes the key window.
  32.  */
  33. {
  34.     inputText = [theScrollView docView];
  35.     [inputText setDelegate:self];
  36.     [[NXApp appListener] setServicesDelegate:self];
  37.     [[thePlotView window] makeKeyAndOrderFront:self]; 
  38.     return self;
  39. }
  40. - NukeCities:sender
  41. {
  42. /* clears the text scroller when the button is pressed*/
  43.     [[inputText selectAll:self] delete:self];
  44. return self;
  45. }
  46.  
  47. - textDidGetKeys:theText isEmpty:(BOOL)flag
  48. /*
  49.  * Responds to a message the Text object sends when its text changes.
  50.  * PlotController causes the PlotView to clear itself when the text changes.
  51.  */
  52. {
  53.     /*return [thePlotView clear:self];*/
  54.     return self;
  55. }
  56.  
  57. - requestPlot:(id)pasteboard userData:(const char *)userData error: (char **)msg
  58. /*
  59.  * Responds to a request from another application for the plotting service.
  60.  * PlotController copies the data from the supplied pasteboard into the Text object
  61.  * and then sends a plot: message to the PlotView.
  62.  */
  63. {
  64.     char    *data, *scratch;
  65.     int     length;
  66.  
  67.     [NXApp activateSelf:NO]; 
  68.     [pasteboard types];
  69.     if ([pasteboard readType:NXAsciiPboardType data:&data length:&length]) {
  70.         if(scratch = malloc(length+1)) {
  71.             strncpy(scratch, data, length);
  72.             scratch[length]='\0';
  73.             [[inputText selectAll:self] replaceSel:scratch];
  74.             free(scratch);
  75.         }
  76.         vm_deallocate(task_self(), (vm_address_t)data, (vm_size_t)length);
  77.         [thePlotView plot:self];
  78.     }
  79.     return self;
  80. }
  81.  
  82.  
  83. - plotView:sender providePoints:(NXStream **)stream
  84. /*
  85.  * Responds to a message the PlotView sends requesting the points to plot.  
  86.  * PlotController responds by giving the PlotView access to the Text 
  87.  * object's stream.
  88.  */
  89. {
  90.     *stream = [inputText stream];
  91.     return self;
  92. }
  93.  
  94. - plotView:sender pointDidChange:(NXPoint *)aPoint
  95. /*
  96.  * Responds to a message the PlotView sends notifying its delegate 
  97.  * that a point has been added.  PlotController responds by adding the point to
  98.  * the end of the list in the Text object.
  99.  */
  100. {
  101.     int    endPos;
  102.     char    buffer[100];
  103.  
  104.     sprintf(buffer, "%d, %d\n", (int)aPoint->x, (int)aPoint->y);
  105.     endPos = [inputText byteLength];
  106.     [inputText setSel:endPos :endPos];
  107.     [inputText scrollSelToVisible];
  108.     [inputText replaceSel:buffer];
  109.     return self;
  110. }
  111.  
  112. @end
  113.  
  114.  
  115.  
  116.